package sysmgr.dao.impl;

import commmon.db.DbConnection;
import commmon.db.JdbcConnectionPool;
import sysmgr.bean.BookCommodity;
import sysmgr.bean.Page;
import sysmgr.bean.StaffAddress;
import sysmgr.dao.AddressDao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class StaffAddressDao implements AddressDao {

        /**
         * 保存数据
         *
         * @param staffDate 用户数据对象
         * @return 是否保存成功
         */
        @Override
        public boolean saveStaffDate(StaffAddress staffDate) {
            return false;
        }

    /**
         * 删除数据
         *
         * @param staffDate 用户数据对象
         * @return 是否保存成功
         */
        @Override
        public boolean removeStaffDate(StaffAddress staffDate) {
            return false;
        }


    /**
         * 查询所有的用户数据对象，携带分页
         *
         * @param page 分页对象
         * @return 封装了用户数据对象的集合
         */
        @Override
        public List<StaffAddress> queryStaffDateAll(Page page, StaffAddress staffAddress) {
            try {
                //创建连接对象
                Connection conn= JdbcConnectionPool.getConnection();
                String sql="select * from staff_address";
                //进行条件的拼接,判断如果不为null且不为空字符串
                if (!"".equals(staffAddress.getStaffId()) && staffAddress.getStaffId() !=null) {
                    //如果传入了条件，就将条件拼接到后面，否则就不拼接
                    sql += " where staffId like '%" + staffAddress.getStaffId() + "%'";
                }
                ResultSet q = (ResultSet) DbConnection.toExecuteMethod(conn, sql, null, "Q");
                Object staff1= DbConnection.getObjectFromResultSetList(q, StaffAddress.class);
                List<StaffAddress> staffDateList = (List<StaffAddress>) staff1;
                if(staff1!=null){
                    return staffDateList;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
    @Override
    public boolean deleteStaffDateAll(StaffAddress staffAddress) {
        try {
            //创建连接对象
            Connection conn= JdbcConnectionPool.getConnection();
            String sql="delete from staff_address where id = " + staffAddress.getId();
            Integer d = (Integer) DbConnection.toExecuteMethod(conn, sql, null, "D");
            if (d == 1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    @Override
    public boolean updateStaffDateAll(StaffAddress staffAddress) {
        try {
            //创建连接对象
            Connection conn = JdbcConnectionPool.getConnection();
            String sql = "update staff_address set detail ='"+staffAddress.getDetail()+"' where id ="+staffAddress.getId();
            Integer u = (Integer) DbConnection.toExecuteMethod(conn, sql, null, "U");
            if (u == 1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
